<%@page import="sql.DBManagment"%>
<%@page import="sql.record.*"%>
<%@page import="java.util.*"%>
<%	
	User loginUser = new User();
	loginUser.userName = (String) session.getAttribute("loggedUser");
	User storedUser = new User();
	try  {
		List<User> userList = DBManagment.getRecord(loginUser);
		storedUser = userList.get(0);
	}catch(Exception e){
		%>
		<script type = "text/javascript" src = "script/end_session.js"></script>		
		<%
	}
 %>
<!--This jsp file is called in case the mng_courses link was pressed.
	It will connect to the db and retrieve all of the courses data -->
	
<%
	List<TableRecord> courseSchedList = DBManagment.getCourseDetails();

	//will get all courses
	List<Course> courses = new LinkedList<Course>();
	for(TableRecord t: courseSchedList){
		if(t instanceof Course){
			int lastIndex = courses.size() - 1;
			Course currentCourse = (Course)t;
			if((lastIndex == -1) || (!courses.get(lastIndex).courseName.equals(currentCourse.courseName))){
				courses.add((Course)t);
			}
		}
	}
%>
<script type="text/javascript" src = "script/forms.js"></script>
<script type="text/javascript" src = "script/mng_courses.js"></script>
<div class="bodytext" style="padding:12px;" align="justify">

	<form name="mngForm" id="mngForm" class="tableForm">
		<fieldset>
			<legend>Control Box</legend>
			<input type="text" id="searchBox" /><br />
			<label for="srchOptions">Also Search By </label>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Credit Points</span>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Capacity</span>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Description</span>
			<br/><br/>
		  	<input type="button" id="markAll" value="Mark All" />
		  	<input type="button" id="UnMarkAll" value="Unmark All" />
			<input type="button" id="deleteMarked" value="Delete Marked" class = "handleMarked"/>
			<!--an add_course.jsp will be activated when the link is clicked  -->
			<input type="button" value="Add Course" id="add_course" title="Add Course" class="menuLink"/>
		</fieldset>
		 
		<!--this will be replicated via loop--->
		<!--creating a panel for each course-->
		<% 
			for(Course c:courses){
		%>
		<div class="panel" align="justify">
			<span class="orangetitle">
				<input type="checkbox" name="mark" id="mark" value=<%="\"" + c.courseName + "\""%> />
		      	<%=c.courseName%>
		      	<input type="hidden" value=<%="\"" + c.courseName + "\""%> class="hiddenData" />
		    </span>
			<div class="bodytext"><br />
				<ul>
					<li><b>Course Name: </b><%=c.courseName%></li>
					<li>
						<b>Credit Points: </b><%=c.creditPoints%>
						<input type="hidden" value=<%="\"" + c.creditPoints + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Capacity: </b><%=c.capacity%>
						<input type="hidden" value=<%="\"" + c.capacity + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Description: </b><br/><%=c.description%>
						<input type="hidden" value=<%="\"" + c.description + "\""%> class="hiddenData" />
					</li>
					<li>
						<strong>Schedule</strong>
							<ul>
							<%
								String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday"};
								for(TableRecord t : courseSchedList){
									if((t instanceof CourseSchedule) && (((CourseSchedule)t).courseName.equals(c.courseName))){
									CourseSchedule currentSched = ((CourseSchedule)t);
								%>
									<%
										String startTime = currentSched.startHour.toString();
										String endTime = currentSched.endHour.toString();
										if(currentSched.startHour.intValue() < 1000){
											startTime = "0" + startTime;
										}
										if(currentSched.endHour.intValue() < 1000){
											endTime = "0" + endTime;
										}							
									%>
									<li><%=days[currentSched.day]%>, <%=startTime%> to <%=endTime%> </li>
								<%		
									}
								}
							%>
							</ul>
					</li>
				</ul>
		  </div>
		</div>
		<%
			}
		%>
	</form>
</div>
